#region Copyright Jonathan Quiros

//	
// All rights are reserved. Reproduction or transmission in whole or in 
// part, in any form or by any means, electronic, mechanical or otherwise, 
// is prohibited without the prior written consent of the copyright owner.
//
// Filename: TeamsLogic.cs
//

#endregion

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Ids.Utils;
using Ids.Web.Listing;
using Novagoal.Models;
using Novagoal.Models.Utils;
using Novagoal.Repositories;

namespace Novagoal.Logic
{
    public class TeamsLogic : BaseLogic
    {
        #region CONSTANTS


        #endregion

        #region PROPERTIES

        private readonly ITeamsRepository _teamsRepository;

        #endregion

        #region PUBLIC METHODS

        public TeamsLogic(ITeamsRepository teamsRepository)
        {
            _teamsRepository = teamsRepository;
        }

        public TeamList GetTeams(NameValueCollection filters, string sort, string direction, int currentPage, int pageSize)
        {
            TeamListSortField sortField;
            SortDirectionType sortDirection;
            Dictionary<TeamListFilter, string> filterList;

            try
            {
                //Sorting based on parameters
                sortField = sort.ToEnumValue<TeamListSortField>();
                sortDirection = direction.ToEnumValue<SortDirectionType>();
                filterList = ParseFilters<TeamListFilter>(filters);
            }
            catch
            {
                //Default Sorting
                filterList = null;
                sortField = TeamListSortField.Name;
                sortDirection = SortDirectionType.Ascending;
            }

            var items = _teamsRepository.GetTeams(filterList, sortField, sortDirection, currentPage, pageSize);
            var count = _teamsRepository.GetTotalTeams(filterList);

            var teamList = new TeamList
            {
                CurrentPage = currentPage,
                Filters = filterList,
                Items = items,
                SortDirection = sortDirection,
                SortField = sortField
            };
            teamList.CalculatePagination(count, pageSize, currentPage);

            return teamList;
        }

        public List<Team> GetTeamsByTournament(int tournamentId)
        {
            return _teamsRepository.GetTeamsByTournament(tournamentId);
        }

        public Team GetTeam(int teamId)
        {
            return _teamsRepository.GetTeam(teamId);
        }

        public int SaveTeam(Team team)
        {
            return team.Id == 0 ? _teamsRepository.AddTeam(team) : _teamsRepository.EditTeam(team);
        }

        public List<Team> GetTeams()
        {
            return _teamsRepository.GetTeams();
        }

        public Dictionary<Team, bool> GetTeamsByPlayer(int playerId)
        {
            return _teamsRepository.GetTeamsByPlayer(playerId);
        }

        public void SavePlayerInTeams(Player player, int[] teamIds, bool[] enabled)
        {
            _teamsRepository.DeleteAllPlayerTeams(player.Id);
            for (var i = 0; i < teamIds.Length && i < enabled.Length; i++ )
            {
                _teamsRepository.AddPlayerInTeam(player, teamIds[i], enabled[i]);
            }
        }

        #endregion

        #region PRIVATE METHODS



        #endregion
    }
}